import axios from 'axios'
export const NAME_LIST_URL = '/IVS/data/config/list/get_template.do'

const state = {
  nameList: []
}

const mutations = {
  SET_NAME_LIST: (state, value) => {
    const result = value.map(item => {
      let memo = item.memo
      try {
        memo = JSON.parse(memo)
      } catch (e) {
        memo = {}
      }
      const id = item.uid
      const name = memo.name
      const id_num = memo.idCard
      const department = memo.department

      return { id, name, id_num, department }
    })
    state.nameList = result
  }
}

const actions = {
  loadNameListFromVuex ({commit}, url = NAME_LIST_URL) {
    axios.get(url).then(res => {
      commit('SET_NAME_LIST', res.data)
    }).catch(() => {
      alert('获取人员信息异常')
    })
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
